package com.pay.ioopos.fragment;

import static com.pay.ioopos.App.SERVER_TYPE_A_PAY;
import static com.pay.ioopos.App.SERVER_TYPE_C_PAY;
import static com.pay.ioopos.AppUtils.serverType;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.pay.ioopos.KeyCodeFactory;
import com.pay.ioopos.KeyInfo;
import com.pay.ioopos.R;
import com.pay.ioopos.support.check.Check;
import com.pay.ioopos.support.check.CheckBind;
import com.pay.ioopos.support.check.CheckBindApay;
import com.pay.ioopos.support.check.CheckBindCpay;
import com.pay.ioopos.support.check.CheckNetwork;
import com.pay.ioopos.support.check.CheckPant;
import com.pay.ioopos.support.check.CheckReport;
import com.pay.ioopos.support.check.CheckReportApay;
import com.pay.ioopos.support.check.CheckReportCpay;
import com.pay.ioopos.support.check.CheckScanNfc;
import com.pay.ioopos.support.check.CheckScanQrcode;
import com.pay.ioopos.support.check.CheckScreen;
import com.pay.ioopos.support.check.CheckSpeech;
import com.pay.ioopos.support.check.CheckVolume;

/**
 * 帮助菜单
 * @author    Moyq5
 * @since  2020/6/16 15:55
 */
public class CheckMenuFragment extends AbstractFragment implements View.OnKeyListener {
    private View view;
    private Check volume;
    private Check speech;
    private Check network;
    private Check bind;
    private Check report;
    private Check screen;
    private Check scanCamera;
    private Check scanNfc;
    private Check pant;
    public CheckMenuFragment(Context context) {
        volume = new CheckVolume(context);
        speech = new CheckSpeech();
        network = new CheckNetwork(context);
        bind = serverType() == SERVER_TYPE_C_PAY
                ? new CheckBindCpay(network): serverType() == SERVER_TYPE_A_PAY
                ? new CheckBindApay(network): new CheckBind(network);
        report = serverType() == SERVER_TYPE_C_PAY
                ? new CheckReportCpay(context, bind) : (serverType() == SERVER_TYPE_A_PAY)
                ? new CheckReportApay(context, bind) : new CheckReport(context, bind);
        screen = new CheckScreen(context);
        scanCamera = new CheckScanQrcode();
        scanNfc = new CheckScanNfc();
        pant = new CheckPant(bind);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_check_menu, container, false);
        view.setFocusable(true);
        view.setOnKeyListener(this);
        view.requestFocus();
        return view;
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() != KeyEvent.ACTION_UP) {
            return true;
        }
        KeyInfo keyInfo = KeyCodeFactory.getKeyInfo(keyCode);
        if (null == keyInfo) {
            return true;
        }
        switch (keyInfo) {
            case KEY_NUM_1:
                setMainFragment(new CheckConsoleFragment(network, bind, report));
                return true;
            case KEY_NUM_2:
                setMainFragment(new CheckConsoleFragment(network));
                return true;
            case KEY_NUM_3:
                setMainFragment(new CheckConsoleFragment(bind));
                return true;
            case KEY_NUM_4:
                setMainFragment(new CheckConsoleFragment(report));
                return true;
            case KEY_NUM_5:
                setMainFragment(new CheckConsoleFragment(screen));
                return true;
            case KEY_NUM_6:
                setMainFragment(new CheckConsoleFragment(scanCamera));
                return true;
            case KEY_NUM_7:
                setMainFragment(new CheckConsoleFragment(scanNfc));
                return true;
            case KEY_NUM_8:
                setMainFragment(new CheckConsoleFragment(volume));
                return true;
            case KEY_NUM_9:
                setMainFragment(new CheckConsoleFragment(speech));
                return true;
            case KEY_NUM_0:
                setMainFragment(new CheckConsoleFragment(pant));
                return true;
        }

        return false;
    }

}
